package com.my.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.my.bean.Checkfresh;
import com.my.bean.ResultPage;
import com.my.bean.User;
import com.my.bean.VoPage;
import com.my.service.CheckfreshService;
import com.my.util.SessionUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 核对生鲜 前端控制器
 * </p>
 *
 * @author 闫亚茹
 * @since 2018-09-08
 */
@Controller
@RequestMapping("/checkfresh")
public class CheckfreshController {
	@Autowired
    CheckfreshService checkfreshService;

    Logger logger=Logger.getLogger(CheckfreshController.class);

    /**
     * 添加
     * @param param
     * @return
     */
    @RequestMapping("/addCheckfresh")
    @ResponseBody
    public  String addCheckfresh(String param,HttpSession session){
        Checkfresh checkfresh= JSON.parseObject(param,Checkfresh.class);
         checkfresh.setCreatetime(new Date().getTime());
        Long areaid=((User)session.getAttribute("user")).getAreaId();
        checkfresh.setAreaid(areaid);
        if (checkfreshService.insert(checkfresh)){
            logger.info("{addCheckfresh|保存成功}");
            return  "保存成功";
        }else{
            logger.info("{addCheckfresh|保存失败}");
            return  "保存失败";
        }
    }
    /**
     * 查询并分页
     * @param voPage
     * @return
     */
    @RequestMapping("/seleCtheckfresh")
    @ResponseBody
    public ResultPage<Checkfresh> selectFiresafety(String lin, String data, VoPage voPage, HttpSession session){
        boolean admin=false;
        //如果事超级管理员  即可查看所有的房间  判断是否为超级管理员
        if(SessionUtil.getSessionUser(session).getRoles().getRoleValue().equals("9f77d5a8-75c6-4d06-adf9-a37e727d491a")){
            admin=true;
        }
        Map map=new HashMap();
        map.put("lin",lin);
        map.put("data",data);
        Long areaid=((User)session.getAttribute("user")).getAreaId();
        ResultPage<Checkfresh> resultPage = checkfreshService.selectMyPage(map,voPage,areaid,admin);
        return resultPage;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("/delectCtheckfresh")
    @ResponseBody
    public boolean delectCtheckfresh(Long id){
        return  checkfreshService.deleteById(id);
    }

    /**
     * 修改
     * @param param
     * @return
     */
    @RequestMapping("/updateCtheckfresh")
    @ResponseBody
    public String updateCtheckfresh(String param){
        Checkfresh  checkfresh = JSONObject.parseObject(param,Checkfresh.class);
        if(checkfreshService.updateById(checkfresh)){
            return  "修改成功";
        }else{
            return "修改失败";
        }

    }

    @RequestMapping("/checkfresh")
    public String  checkfresh(){
        return "checkfresh";
    }
    @RequestMapping("/checkfresh_Add")
    public String  checkfresh_Add(){
        return "Alert/checkfresh_Add";
    }

    @RequestMapping("/checkfresh_Edit")
    public String  checkfresh_Edit(){
        return "Alert/checkfresh_Edit";
    }




}
